<template>
  <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="700px" :close-on-click-modal="false"
    :before-close="handleClose">
    <div class="form">
      <!-- 表单输入元件 ----------------------------------------------------------------------------------------->
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="rule-form">
        <el-form-item label="标准名称" prop="levelName">
          <el-input v-model="ruleForm.levelName" placeholder="请输入供应商名称" />
        </el-form-item>

        <el-form-item label="验收类型" prop="acceptanceType">
          <el-select v-model="ruleForm.acceptanceType" filterable placeholder="请选择验收类型">
            <el-option v-for="item in DictionaryList" :key="item.dictValue" :label="item.dictLabel"
              :value="item.dictValue + ''" />
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" placeholder="请输入备注" v-model="ruleForm.remark" maxlength="50" show-word-limit>
          </el-input>
        </el-form-item>

      </el-form>
      <!-- ----------------------------------------------------------------------------------------------------->
    </div>
    <div class="dialog_footer">
      <el-button type="primary" @click="handleConfirm">提交</el-button>
      <el-button @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** 远程添加，修改，获取单个详性接口，获取selector下拉列表的接口，以及时间转换工具函数等 ------------------------------------------------- */
import { standardAdd, standardUpdate, standardDetail } from '@/api/purchasing/equipment'
// 获取字典信息-------------------------------------------------------------------------------------
import { listDictionary } from '@/api/menu.js'
/** ------------------------------------------------------------------------------------------------------------------------------- */

/** 表单参数 ------------------------------------------------------------------------------------------------------------------------ */
const ruleForm = {
  levelName: "", //验证标准名称
  acceptanceType: null, //验证类型
  remark: "" //备注
}
/** ---------------------------------------------------------------------------------------------------------------------------------- */

export default {
  name: 'Form',
  data () {
    return {
      dialogVisible: false,
      ruleForm: Object.assign({}, ruleForm),
      isUpdateForm: false,
      dialogTitle: '添加验收标准',
      DictionaryList: [],
      /** 表单规则 --------------------------------------------------------------------------------------------------------------------- */
      rules: {
        levelName: [
          { required: true, message: '请输入验证标准名称', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        acceptanceType: [
          { required: true, message: '请选择验证类型', trigger: 'change' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      }
      /** ----------------------------------------------------------------------------------------------------------------------------- */
    }
  },
  created () {
    this.handleListType()
  },
  methods: {
    // 类型和字典单位数据接口---------------------------------------------
    async handleListType () {
      const { data: dictionary } = await listDictionary({ dictType: 'acceptance_level', status: 0 })//字典单位
      this.DictionaryList = dictionary
    },

    // 打开表单对话框
    openDialog (id = null) {
      if (id) {
        standardDetail(id).then(res => {
          this.isUpdateForm = true
          this.dialogTitle = '修改验收标准'
          /** 修改时的表单参数赋值 --------------------------------------------------------------------------------------------------------------------- */
          this.ruleForm = res.data
          this.dialogVisible = true
        })
      } else {
        this.isUpdateForm = false
        this.dialogTitle = '添加验收标准'
        this.dialogVisible = true
      }
    },
    // 提交表单
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = this.ruleForm
          console.log('提交的表单', data)
          if (this.isUpdateForm) {
            standardUpdate(data).then((res) => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.resetForm('ruleForm')
              this.dialogVisible = false
              this.$emit('submit_success')
            })
          } else {
            standardAdd(data).then((res) => {
              this.$message({
                message: '创建成功',
                type: 'success'
              })
              this.fileList = []
              this.$nextTick(function () {
                this.$refs['ruleForm'].clearValidate()
              });
              this.resetForm('ruleForm')
              this.dialogVisible = false
              this.$emit('submit_success')
            })
          }
        } else {
          this.$message({
            message: '数据验证不通过，检查输入表单',
            type: 'error'
          })
          return false
        }
      })
    },
    // 重置表单
    resetForm (formName) {
      if (this.isUpdateForm) {
        this.$refs[formName].resetFields()
      } else {
        this.ruleForm = Object.assign({}, ruleForm)
        this.fileList = []
      }
      // --------consolelog('重置表单', this.ruleForm)
    },
    /** ---------------------------------------------------------------------------------------------------------------------------------------- */
    // 关闭窗口，重置表单
    handleClose () {
      this.ruleForm = Object.assign({}, ruleForm)
      this.dialogVisible = false
      this.fileList = []
      this.$nextTick(function () {
        this.$refs['ruleForm'].clearValidate()
      });
    },
    handleConfirm () {
      this.submitForm('ruleForm')
    },
    handleReset () {
      this.resetForm('ruleForm')
    }
  }
}
</script>
<style lang="scss" scoped>
.form {
  width: 500px;
  margin-left: auto;
  margin-right: auto;

  .inventory {
    display: flex;
  }
}

::v-deep .el-input-number__decrease {
  display: none;
}

::v-deep .el-input-number__increase {
  display: none;
}

::v-deep .el-select {
  width: 100%;
}

::v-deep .el-input-number {
  width: 100%;
}

.dialog_footer {
  display: flex;
  justify-content: center;
}
</style>
